'use strict';
import { Controller } from 'egg';

export default class GoodsController extends Controller {
  public async publishGoods() {
    const { ctx } = this;
    try {
      let res = await ctx.service.goods.addGoods(ctx.request.body);
      ctx.success(res, '发布成功');
    } catch (e) {
      console.log(e);
      ctx.error(423, ctx.helper.errorCode[423]);
    }
  }

  public async updateGoods() {
    const { ctx } = this;
    try {
      let res = await ctx.service.goods.updateGoods(ctx.request.body);
      ctx.success(res, '发布成功');
    } catch (e) {
      console.log(e);
      ctx.error(423, ctx.helper.errorCode[423]);
    }
  }

  public async delGoods () {
    const { ctx } = this;
    try {
      console.log(ctx.query);
      let res = await ctx.service.goods.delGoods(ctx.query);
      ctx.success(res, '删除成功');
    } catch (e) {
      console.log(e);
      ctx.error(423, ctx.helper.errorCode[423]);
    }
  }

  public async getGoods() {
    const { ctx } = this;
    try {
      // console.log(ctx.query);
      let res = await ctx.service.goods.getGoods(ctx.query);
      ctx.success(res, '查询成功');
    } catch (e) {
      console.log(e);
      ctx.error(423, ctx.helper.errorCode[423]);
    }
  }

  public async getFavorites() {
    const { ctx } = this;
    try {
      // console.log(ctx.query);
      let res = await ctx.service.goods.getFavorites(ctx.query);

      ctx.success(res, '查询成功');
    } catch (e) {
      console.log(e);
      ctx.error(423, ctx.helper.errorCode[423]);
    }
  }

  public async getProductList() {
    const { ctx } = this;
    try {
      // console.log(ctx.query);
      let res = await ctx.service.goods.getProductList(ctx.query);
      if (ctx.query.sort == '3') {
        // console.log(res);
      }
      ctx.success(res, '查询成功');
    } catch (e) {
      console.log(e);
      ctx.error(423, ctx.helper.errorCode[423]);
    }
  }

}